//
//  DatabaseHelper.m
//  qiuShiJiangHu
//
//  Created by xalo on 16/1/26.
//  Copyright © 2016年 糗事江湖项目组. All rights reserved.
//

#import "DatabaseHelper.h"
#import "FMDB/FMDB.h"
#import "HomePageModel.h"
static DatabaseHelper *handle = nil;

@interface DatabaseHelper ()

@property(nonatomic,strong)FMDatabase *db;

@end


@implementation DatabaseHelper


+(DatabaseHelper *)sharedManager {
    
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[DatabaseHelper alloc]init];
    });
    
    
    return handle;
    
}

- (instancetype)init {
    
    self = [super init];
    if (self) {
        NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
        
        NSString *dbPath = [documentPath stringByAppendingPathComponent:@"db.sqlite"];
        self.db = [FMDatabase databaseWithPath:dbPath];
        //打开数据库
        [self.db open];
        //创建表
        if ([self.db open]) {
            //sql语句
            NSString *sql = @"create table 'database'(sid integer primary key autoincrement not null,data blob);";
            //创建表
            BOOL result = [self.db executeUpdate:sql];
            if (result) {
                NSLog(@"表创建成功");
            }
        }
        //关闭数据库
        [self.db close];
        
    }
    
    
    return self;
    
}

//添加到数据库
- (BOOL)insertToSqlitWithModel:(HomePageModel *)model Id:(NSString *)Id {
    
    //归档
    NSData *data = [DatabaseHelper archiverWithObject:model key:Id];
    //打开数据库
    [self.db open];
    //sql语句
    NSString *sql = @"insert into 'database'(model,Id) values(?,?);";
    //执行
    BOOL result = [self.db executeUpdate:sql,data,Id];
    //关闭数据库
    [self.db close];
    
    return result;
}

//根据Id删除
- (BOOL)deleteWithId:(NSString *)Id {
    
    //sql语句
    NSString *sql = @"delete from 'database' where Id = ?;";
    //打开数据库
    [self.db open];
    //执行
    BOOL result = [self.db executeUpdate:sql,Id];
    //关闭数据库
    [self.db close];
    
    return result;
}

//全部删除
- (BOOL)deleteAll {
    
    //sql 语句
    NSString *sql = @"delete from 'database';";
    //打开数据库
    [self.db open];
    //执行
    BOOL result = [self.db executeUpdate:sql];
    //关闭数据库
    [self.db close];
    
    return result;
    
}

//根据Id查找
- (HomePageModel *)selectWithId:(NSString *)Id {
    
    NSString *sql = [NSString stringWithFormat:@"select *from 'database' where Id = '%@';",Id];
    //打开数据库
    [self.db open];
    
    FMResultSet *Fresult = [self.db executeQuery:sql];
    
    HomePageModel *model = [[HomePageModel alloc]init];
    
    while ([Fresult next]) {
        NSData *data = [Fresult dataForColumnIndex:1];
        NSString *Id = [Fresult stringForColumnIndex:2];
        
        //反归档
        model = [DatabaseHelper unarchiverWithData:data key:Id];
        
    }
    
    [Fresult close];
    
    //关闭数据库
    [self.db close];
    
    return model;
    
}

//查找全部

- (NSArray *)selectAll {
    
    NSMutableArray *array = [NSMutableArray array];
    NSString *sql = @"select *from 'database';";
    //打开数据库
    [self.db open];
    
    FMResultSet *Fresult = [self.db executeQuery:sql];
    
    while ([Fresult next]) {
        NSData *data = [Fresult dataForColumnIndex:1];
        NSString *Id = [Fresult stringForColumnIndex:2];
        
        //反归档
        HomePageModel *model = [DatabaseHelper unarchiverWithData:data key:Id];
        [array addObject:model];
    }
    [Fresult next];
    
    //关闭数据库
    [self.db close];
    
    return array;
}





//归档
+ (NSData *)archiverWithObject:(id)object key:(NSString *)key {
    
    NSMutableData *data = [NSMutableData data];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
    [archiver encodeObject:object forKey:key];
    [archiver finishEncoding];
    return data;
    
}


//反归档

+ (id)unarchiverWithData:(NSData *)data key:(NSString *)key {
    
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
    id object = [unarchiver decodeObjectForKey:key];
    [unarchiver finishDecoding];
    
    return object;
    
}












@end
